package com.ty.web.push;

import com.ty.model.SocketMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.user.SimpUserRegistry;

/**
 * TPush消息推送
 *
 * @Author Tommy
 * @Date 2024/4/18
 */
public class TPush {

    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    @Autowired
    private SimpUserRegistry userRegistry;

    /**
     * 广播消息
     *
     * @param destination
     * @param message
     */
    public void send(String destination, SocketMessage message) {
        if (StringUtils.isNotBlank(destination) && null != message) {
            messagingTemplate.convertAndSend(destination, message);
        }
    }

    /**
     * 点对点消息
     *
     * @param user
     * @param destination
     * @param message
     */
    public void sendToUser(String user, String destination, SocketMessage message) {
        if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(destination) && null != message) {
            messagingTemplate.convertAndSendToUser(user, destination, message);
        }
    }
}
